<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px">3天前</span>
            <van-button size="mini" type="default" @click="showw(item)">
              回复{{ item.reply_count }}
            </van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          @click="like(item)"
          :name="item.is_liking ? 'like' : 'like-o'"
        />
      </van-cell>
    </van-list>
    <!-- /评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容" v-model="zi">
        <van-button slot="button" size="mini" type="info" @click="fabu">
          发布
        </van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <van-action-sheet v-model="show" title="标题">
      <pinglunhuifu :obj="mianban" :id="artId"></pinglunhuifu>
    </van-action-sheet>
  </div>
</template>

<script>
import pinglunhuifu from '@/views/article/pinglunhuifu.vue'
import {
  getArticleCommon,
  addArticleCommon,
  likeCommon,
  unLikeCommon
} from '@/api/article'
export default {
  components: {
    pinglunhuifu
  },
  props: {},
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      zi: '',
      show: false,
      mianban: ''
    }
  },
  computed: {
    artId() {
      return this.$route.params.id
    }
  },

  methods: {
    showw(item) {
      console.log(item)
      this.show = true
      this.mianban = item
    },
    async like(item) {
      if (item.is_liking) {
        await unLikeCommon(item.com_id)
      } else {
        const { data: res } = await likeCommon(item.com_id)
        console.log(res)
      }
    },

    async fabu() {
      const { data: res } = await addArticleCommon(this.artId, this.zi)
      // console.log(res)
      this.list.unshift(res.new_obj)
    },
    onLoad() {
      console.log(this.artId)
      // 异步更新数据
      setTimeout(async () => {
        const { data: res } = await getArticleCommon(this.artId, this.offset)
        // console.log(res)
        this.list = [...this.list, ...res.results]
        // 加载状态结束
        this.loading = false
        this.offset = res.last_id
        // 数据全部加载完成
        if (res.results.length === 0) {
          this.finished = true
        }
      }, 500)
    }
  }
}
</script>

<style scoped lang="less">
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
